<?php

namespace app\controller\api;

use app\model\api\Mini as MiniModel;
use laytp\controller\Api;
use think\exception\ValidateException;

/**
 * Class Mini
 * @package app\index\controller
 */
class Mini extends Api
{
    protected $model;

    protected function initialize()
    {
        $this->model = new MiniModel();
    }

    /**
     * 迷你世界图鉴
     * @return false|string|\think\response\Json
     */
    public function list()
    {
        $data = input("post.");
        $keyword = !empty($data['keyword']) ? $data['keyword']:"";
        $type = !empty($data['primary_type']) ? $data["primary_type"]:"";
        $second_type = !empty($data['second_type']) ? $data["second_type"]:"";
        $page = !empty($data['page']) ? $data['page'] : 1;
        try {
            //验证器
            validate(\app\validate\api\Mini::class)
                ->scene('list')
                ->check([
                    'page' => $page
                ]);
            $list = $this->model->list($keyword, $type,$second_type);
            return $this->success('success', $list['data'], array(
                'total' => $list['total'],
                'current_page' => $list['current_page'],
                'last_page' => $list['last_page'],
                'per_page' => $list['per_page']
            ));

        } catch (ValidateException $exception) {
            return $this->error($exception->getMessage(), -1);
        }
    }

    /**
     * 详情
     * @return false|string|\think\response\Json
     */
    public function detail()
    {
        $data = input('post.');
        $record_id = !empty($data['record_id']) ? $data['record_id']:'';
        try {
            //验证
            validate(\app\validate\api\Mini::class)
                ->scene('detail')
                ->check([
                    'record_id' => $record_id
                ]);
            $detail = $this->model->detail($record_id);
            return $this->success('success', $detail);
        } catch (ValidateException $exception) {
            return $this->error($exception->getMessage(), -1);
        }
    }


    /**
     * 分类
     * @return false|string|\think\response\Json
     */
    public function type()
    {
        try {
            $typeList = $this->model->typeList();
            return $this->success('success', $typeList);
        } catch (ValidateException $exception) {
            return $this->error($exception->getMessage(), -1);
        }
    }
}